package com.boot.ys.mybatisplus.script;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;

/**
 * MP4 视频批量添加文字水印
 *
 * @Author yangsheng
 * @Date 2024/11/2 14:47
 */
public class MoreVideAddText {

    public static void main(String[] args) {

        HashMap<String, String> map = new HashMap<>();
        // key 输入目录 value 输出目录
        map.put("E:\\BaiduNetdiskDownload\\上传h5商城数据库的图片\\weichat3.0", "E:\\BaiduNetdiskDownload\\上传h5商城数据库的图片\\3.0水印微信小程序");

        for (String key : map.keySet()) {
            // 输入指定视频文件的根目录
            String videoDirectory = key;
            // 输出视频目录
            String outputDirectory = map.get(key);

            String textToAdd = "微信公众号：阿拉升 源码分享网址：www.alsymdd.cn"; // 根据需要修改
            String ffmpegPath = "D:\\software\\FFmpeg\\ffmpeg-7.1-essentials_build\\bin\\ffmpeg.exe";
            // 试了很多次这里要用相对路径，不然会找不到字体文件
            String fontFilePath = "src/main/resources/fonts/simhei.ttf";

            // 创建输出目录
            new File(outputDirectory).mkdirs();

            processDirectory(videoDirectory, outputDirectory, textToAdd, ffmpegPath, fontFilePath);
        }
    }

    private static void processDirectory(String videoDirectory, String outputDirectory, String textToAdd, String ffmpegPath, String fontFilePath) {
        File dir = new File(videoDirectory);
        File[] files = dir.listFiles();

        if (files != null) {
            for (File file : files) {
                if (file.isDirectory()) {
                    // 递归处理子文件夹
                    String newOutputDirectory = outputDirectory + File.separator + file.getName();
                    new File(newOutputDirectory).mkdirs();
                    processDirectory(file.getAbsolutePath(), newOutputDirectory, textToAdd, ffmpegPath, fontFilePath);
                } else if (file.isFile() && file.getName().endsWith(".mp4")) {
                    // 处理视频文件
                    String outputFilePath = outputDirectory + File.separator + file.getName();
                    String command = String.format("\"%s\" -i \"%s\" -vf \"drawtext=text='%s':fontcolor=red:fontsize=50:x=(w-text_w-10):y=(h-text_h-10):fontfile='%s'\" -c:v libx264 -crf 18 -preset veryfast -c:a copy \"%s\"",
                            ffmpegPath, file.getAbsolutePath(), textToAdd, fontFilePath, outputFilePath);

                    System.out.println("执行命令: " + command);
                    try {
                        Process process = Runtime.getRuntime().exec(command);

                        // 启动线程读取标准输出
                        new Thread(() -> {
                            try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
                                String line;
                                while ((line = reader.readLine()) != null) {
                                    System.out.println(line); // 输出信息
                                }
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }).start();

                        // 启动线程读取错误输出
                        new Thread(() -> {
                            try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()))) {
                                String line;
                                while ((line = reader.readLine()) != null) {
                                    System.err.println(line); // 输出错误信息
                                }
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }).start();

                        // 检查退出代码
                        int exitCode = process.waitFor();
                        if (exitCode == 0) {
                            System.out.println("处理成功: " + file.getName());
                        } else {
                            System.err.println("处理失败: " + file.getName() + "，退出代码: " + exitCode);
                        }
                    } catch (IOException | InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        } else {
            System.out.println("没有找到视频文件或子文件夹。");
        }
    }
}

